package com.hsnn.medstgmini.pro.project.controller;

import java.util.List;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hsnn.medstgmini.Constants;
import com.hsnn.medstgmini.pro.project.model.ContractModel;
import com.hsnn.medstgmini.pro.project.model.ContractModelLog;
import com.hsnn.medstgmini.pro.project.service.ContractModelManager;
import com.hsnn.medstgmini.sys.model.SysUser;
import com.hsnn.medstgmini.sys.service.SysUserManager;
import com.hsnn.medstgmini.util.DateUtil;
import com.hsnn.medstgmini.util.Pagination;

@Controller
@RequestMapping("contractModel")
public class ContractModelController {
	
	@Autowired
	private ContractModelManager contractModelManager;
	
	@Autowired
	private HttpServletRequest request;
	
	@Autowired
	private SysUserManager sysUserManager;
	
	private static final Logger log = Logger
			.getLogger(ContractModelController.class);
	
	@Value("#{sysProp['system.swftoolspath']}")
	private String swftoolspath;
	
	/**
	 * @category 跳转到合同模板列表页面
	 * @author zhouqi
	 * @date 2015年9月8日14:18:48
	 * @return string
	 */
	@RequestMapping("toContractModelList")
	public String toContractModelList(){
		return "/pro/project/contractModelList";
	}
	
	/**
	 * @category 获取合同模板页面列表
	 * @author zhouqi
	 * @date 2015年9月8日14:18:48
	 * @param contractModelName 模板名称
	 * @param status 状态
	 * @return string
	 */
	@RequestMapping("getContractModelList")
	@ResponseBody
	public Pagination getContractModelList(String contractModelName, String status,String contractModelCode){
		Pagination page = new Pagination(request);
		if(StringUtils.isNotBlank(contractModelName)){
			page.getConditions().put("contractModelName", contractModelName);
		}
		if(StringUtils.isNotBlank(contractModelCode)){
			page.getConditions().put("contractModelCode", contractModelCode);
		}
		if(StringUtils.isNotBlank(status)){
			page.getConditions().put("status", status);
		}
		try{
			contractModelManager.getContractModelListPage(page);
		}catch(Exception e){
			page.setSuccess(false);
			log.error("",e);
		}
		return page;
	}
	
	/**
	 * @category 跳转到编辑或新增合同模板
	 * @author zhouqi
	 * @date 2015年9月8日15:48:31
	 * @param contractModelId 模板id
	 * @param optType 操作类型 新增或修改
	 * @param status 状态
	 * @return string
	 */
	@RequestMapping("toContractModelEdit")
	public String toEditContractModel(Model model, String contractModelId, String optType){
		if(StringUtils.isNotBlank(contractModelId)){
			model.addAttribute("contractModelId", contractModelId);
		}
		if(StringUtils.isNotBlank(optType)){
			model.addAttribute("optType", optType);
			if(Integer.parseInt(optType) == Constants.OP_TYPE_EDIT){
				ContractModel contractModel = contractModelManager.getContractModelById(contractModelId);
				model.addAttribute("contractModel", contractModel);
			}
		}
		return "/pro/project/contractModelEdit";
	}
	
	/**
	 * @category 保存或更新合同模板
	 * @author zhouqi
	 * @date 2015年9月8日14:18:48
	 * @param contractModel 模板类
	 * @param optType 操作类型
	 * @return string
	 */
	@RequestMapping("saveOrUpdContractModel")
	@ResponseBody
	public Pagination saveOrUpdContractModel(ContractModel contractModel, String optType){
		Pagination page = new Pagination();
		SysUser user = (SysUser) request.getSession().getAttribute(Constants.USERINFO);
		String txtContent = request.getParameter("txtContent");
		if(StringUtils.isBlank(optType)||null == contractModel){
			page.setSuccess(false);
			page.setMsg("保存失败");
			return page;
		}
		try{
			Integer type = Integer.parseInt(optType);
			if(type==Constants.OP_TYPE_ADD){
				contractModel.setContractModelId(UUID.randomUUID().toString());//设置id
				contractModel.setContractModelCode(contractModelManager.getContractModelCode());
				contractModel.setStatus(1);
			}
			if(type == Constants.OP_TYPE_EDIT){
				ContractModel oldModel = contractModelManager.getContractModelById(contractModel.getContractModelId());
				contractModel.setContractModelCode(oldModel.getContractModelCode());
				contractModel.setStatus(oldModel.getStatus());
			}
			contractModel.setUpdUser(user.getId());
			contractModel.setUpdDatetime(DateUtil.getTimeStamp());
			contractModel = contractModelManager.genePdfAndSwf(contractModel, request);//生成pdf与swf与路径
			contractModelManager.saveOrUpdateContractModel(type, contractModel);
			contractModelManager.writeLog(contractModel);//写日志
			page.setSuccess(true);
			page.setMsg("保存成功");
		}catch(Exception e){
			page.setSuccess(false);
			page.setMsg("保存失败");
			log.error("",e);
		}
		return page;
	}
	
	/**
	 * @category 改变状态
	 * @author zhouqi
	 * @param ids
	 * @param status
	 */
	@RequestMapping("changeContractNoticeStatus")
	@ResponseBody
	public Pagination changeContractNoticeStatus(String overId, Integer status){
		Pagination page = new Pagination();
		SysUser user = (SysUser) request.getSession().getAttribute(Constants.USERINFO);
		if(null==status||StringUtils.isBlank(overId)){
			page.setSuccess(false);
			page.setMsg("状态修改失败");
			return page;
		}
		try{
			String[] ids = overId.split(",");
			for(int i = 0; i < ids.length; i++){
				ContractModel contractModel  = contractModelManager.getContractModelById(ids[i]);
				contractModel.setStatus(status);
				contractModel.setUpdUser(user.getId());
				contractModel.setUpdDatetime(DateUtil.getTimeStamp());
				contractModelManager.saveOrUpdateContractModel(Constants.OP_TYPE_EDIT, contractModel);
				contractModelManager.writeLog(contractModel);//写日志
			}
			page.setSuccess(true);
			page.setMsg("状态修改成功");
		}catch(Exception e){
			e.printStackTrace();
			page.setSuccess(false);
			page.setMsg("状态修改失败");
		}
		return page;
	}
	
	/**
	 * @category 跳转到合同模板页面详情
	 * @author zhouqi
	 * @date 2015年9月8日18:57:00
	 * @param contractModelId 模板id
	 * @param model
	 * @return string
	 */
	@RequestMapping("toContractModelDetail")
	public String toContractModelDetail(String contractModelId, Model model){
		if(StringUtils.isNotBlank(contractModelId)){
			ContractModel contractModel = contractModelManager.getContractModelById(contractModelId);
			model.addAttribute("contractModel",contractModel);	
		}
		return "/pro/project/contractModelDetail";
	}
	
	/**
	 * @category 跳转到合同模板日志列表
	 * @author zhouqi
	 * @date 2015年9月8日14:18:48
	 * @return string
	 */
	@RequestMapping("toContractModelLogList")
	public String toContractModelLogList(Model model, String contractModelId){
		if(StringUtils.isNotBlank(contractModelId)){
			model.addAttribute("contractModelId", contractModelId);
		}
		return "/pro/project/contractModelLogList";
	}
	
	/**
	 * @category 获取合同模板日志列表
	 * @author zhouqi
	 * @date 2015年9月8日18:57:59
	 * @param startPoint
	 * @param endPoint
	 * @return string
	 */
	@RequestMapping("getContractModelLogList")
	@ResponseBody
	public Pagination getContractModelLogList(String startPoint, String endPoint){
		Pagination page = new Pagination(request);
		String contractModelId = request.getParameter("contractModelId");
		if(StringUtils.isNotBlank(startPoint)){
			page.getConditions().put("startPoint",startPoint);
		}
		if(StringUtils.isNotBlank(endPoint)){
			page.getConditions().put("endPoint",endPoint);
		}
		if(StringUtils.isNotBlank(contractModelId)){
			page.getConditions().put("contractModelId",contractModelId);
		}
		try{
			contractModelManager.getContractModelLogListPage(page);
		}catch(Exception e){
			log.error("",e);
			page.setSuccess(true);
		}
		return page;
	}

	/**
	 * @category 跳转到合同模板日志详情
	 * @author zhouqi
	 * @date 2015年9月8日18:57:59
	 * @param startPoint
	 * @param endPoint
	 * @return string
	 */
	@RequestMapping("toContractModelLogDetail")
	public String toContractModelLogDetail(Model model){
		String contractModelId = request.getParameter("contractModelId");
		String updDateTime = request.getParameter("updDateTime");
		List<ContractModelLog> contractModelList 
		              = contractModelManager.getCompareContactModelLogList(contractModelId, updDateTime);
		ContractModelLog oldContractModelLog = new ContractModelLog();
		ContractModelLog newContractModelLog = new ContractModelLog();
		if(contractModelList.size()==1){
			oldContractModelLog = contractModelList.get(0);
			newContractModelLog = contractModelList.get(0);
		}
		if(contractModelList.size() >1){
			oldContractModelLog = contractModelList.get(0);
			newContractModelLog = contractModelList.get(1);
		}
		//转换日期
		String oldUpdDatetime = DateUtil.dateTime(oldContractModelLog.getUpdDatetime());
		String newUpdDatetime = DateUtil.dateTime(newContractModelLog.getUpdDatetime());
		//状态值转换
		String oldStatus = "";
		String newStatus = "";
		if(null!=oldContractModelLog.getStatus()&&null!=newContractModelLog.getStatus()){
			 oldStatus = oldContractModelLog.getStatus()==1?"启用":"停用";
			 newStatus = newContractModelLog.getStatus()==1?"启用":"停用";
		}
		String oldUpdUserId = oldContractModelLog.getUpdUser();
		String newUpdUserId = newContractModelLog.getUpdUser();
		SysUser oldSysUser = sysUserManager.getUserById(oldUpdUserId);
		SysUser newSysUser = sysUserManager.getUserById(newUpdUserId);
		if(null!=oldSysUser){
			model.addAttribute("oldUpdUser",oldSysUser.getName());
		}
		if(null!=newSysUser){
			model.addAttribute("newUpdUser",newSysUser.getName());
		}
		model.addAttribute("oldContractModelLog", oldContractModelLog);
		model.addAttribute("newContractModelLog", newContractModelLog);
		model.addAttribute("oldUpdDatetime", oldUpdDatetime);
		model.addAttribute("newUpdDatetime", newUpdDatetime);
		model.addAttribute("oldStatus", oldStatus);
		model.addAttribute("newStatus", newStatus);
		model.addAttribute("contractModelId",contractModelId);
		return "/pro/project/contractModelLogDetail";
	}
}
